<template>
  <div class="login-container">
    <div class="login-page">
      <h2 class="login-title">系统登录</h2>
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules" size="medium">
        <el-form-item prop="username">
          <el-input placeholder="用户名" v-model="loginForm.username" @keyup.enter.native="login"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="密码" v-model="loginForm.password" type="password" @keyup.enter.native="login"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入登录名', trigger: 'blur' }, { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        this.$http.post('login', this.loginForm).then(resp => {
          sessionStorage.setItem('id', resp.data.id)
          sessionStorage.setItem('name', resp.data.name)
          sessionStorage.setItem('token', resp.data.token)
          this.$router.push('/')
        })
      })
    }
  }
}
</script>

<style lang="less" scoped="scoped">
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: darkcyan;
}
.login-page {
  width: 350px;
  border: 1px solid #eaeaea;
  padding: 35px 35px 15px;
  border-radius: 5px;
  box-shadow: 0 0 25px #cac6c6;
  -webkit-border-radius: 5px;
  background-color: #fff;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
}
</style>
